<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;
    public $email, $authFrom;

    /**
     * Constructor.
     * @param string $email email
     * @param string $password password
     */
    public function __construct($email, $password)
    {
        $this->email = $email;
        $this->password = $password;
    }

    /**
     * Authenticates a user.
     * The example implementation makes sure if the email and password
     * are 'admin@admin.com' and 'admin'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        $user_model = new User();
        $row = $user_model->isExists($this->email, $this->password);
        if ($row !== false) {
            $this->_id=$row['id'];
            $this->username=$row['first_name']." ".$row['second_name'];
            $this->setAuthFrom('frontend');
            $this->errorCode=self::ERROR_NONE;
        } else
            $this->errorCode=self::ERROR_PASSWORD_INVALID;

        return !$this->errorCode;
    }

    /**
     * Simple authentication for after-payment callbacks
     * 
     * @return bool
     */
    public function simpleAuthenticate()
    {
        $user_model = new User();
        $row = $user_model->isExistsEmail($this->email);
        if ($row !== false) {
            $this->_id = $row['id'];
            $this->username = $row['first_name'] . " " . $row['second_name'];
            $this->setAuthFrom('frontend');
            $this->errorCode = self::ERROR_NONE;
        } else
            $this->errorCode = self::ERROR_PASSWORD_INVALID;

        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getAuthFrom()
    {
        return $this->authFrom;
    }

    public function setAuthFrom($value)
    {
        $this->authFrom=$value;
    }
}